package org.wmq.controller;

import org.wmq.Mapper.impl.MySimpleMapMapperImpl;
import org.wmq.model.Entity.MySimpleMap;
import org.wmq.service.MySimpleMapService;
import org.wmq.service.impl.MySimpleMapServiceImpl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ViewController {
    private static List<MySimpleMap> dataList = new LinkedList<>();
    private static int index = 0;

    public static MySimpleMap getByIndex(){
        return dataList.get(index);
    }

    public static void deleteByIndex(){
        dataList.remove(index);
    }

    public static MySimpleMap getNext(){
        if (index == dataList.size() - 1){
            return getFirst();
        }
        index++;
        return getByIndex();
    }
    public static MySimpleMap getPrevious(){
        if (index == 0){
            return getLast();
        }
        index--;
        return getByIndex();
    }
    public static MySimpleMap getLast(){
        index = dataList.size() - 1;
        return getByIndex();
    }
    public static MySimpleMap getFirst(){
        index = 0;
        return getByIndex();
    }
    public static void resetDataList(List<MySimpleMap> dataList){
        ViewController.dataList = dataList;
    }

    public static void addDataToList(MySimpleMap mySimpleMap) {
        dataList.add(mySimpleMap);
    }
    public void reloadDataList() throws IOException {
        String mySimpleMapFilePrefix = MySimpleMapMapperImpl.MySimpleMapFilePrefix;
        File filePrefix = new File(mySimpleMapFilePrefix);
        for (String filename : filePrefix.list()){
            long id = Long.parseLong(filename.substring(0,filename.indexOf(".")));
            MySimpleMapService mySimpleMapService = new MySimpleMapServiceImpl();
            dataList.add(mySimpleMapService.selectById(id));
        }
    }
}
